from django.db import models
from django.utils import timezone
from accounts.models import User
# 不再导入BlindBox模型
# from blindbox.models import BlindBox

class UserPreference(models.Model):
    """用户偏好模型，存储用户的喜好特征向量"""
    id = models.BigAutoField(primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='preferences')
    preference_data = models.TextField(help_text='用户偏好的JSON向量数据')
    update_time = models.DateTimeField(auto_now=True)
    
    class Meta:
        db_table = 'tb_user_preference'
        verbose_name = '用户偏好'
        verbose_name_plural = '用户偏好'
        managed = False
    
    def __str__(self):
        return f"用户 {self.user_id} 的偏好"

class BoxFeature(models.Model):
    """盲盒特征模型，存储盲盒的特征向量"""
    id = models.BigAutoField(primary_key=True)
    # 添加默认值
    box_id = models.BigIntegerField(default=0, help_text='盲盒ID')
    feature_data = models.TextField(help_text='盲盒特征的JSON向量数据')
    update_time = models.DateTimeField(auto_now=True)
    
    class Meta:
        db_table = 'tb_box_feature'
        verbose_name = '盲盒特征'
        verbose_name_plural = '盲盒特征'
        managed = False
    
    def __str__(self):
        return f"盲盒 {self.box_id} 的特征"

class UserRecommendation(models.Model):
    """用户推荐模型，存储为每个用户推荐的盲盒"""
    id = models.BigAutoField(primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='recommendations')
    # 添加默认值
    box_id = models.BigIntegerField(default=0, help_text='盲盒ID')
    score = models.FloatField(help_text='推荐分数，越高表示越推荐')
    rank = models.IntegerField(default=0, help_text='推荐排名')
    reason = models.CharField(max_length=255, blank=True, null=True, help_text='推荐理由')
    create_time = models.DateTimeField(default=timezone.now)
    click_count = models.IntegerField(default=0, help_text='点击次数')
    last_click_time = models.DateTimeField(null=True, blank=True, help_text='最后点击时间')
    
    class Meta:
        db_table = 'tb_user_recommendation'
        verbose_name = '用户推荐'
        verbose_name_plural = '用户推荐'
        managed = False
        indexes = [
            models.Index(fields=['user', 'score']),
            models.Index(fields=['create_time']),
        ]
    
    def __str__(self):
        return f"给用户 {self.user_id} 推荐的盲盒 {self.box_id}"

class RecommendationLog(models.Model):
    """推荐日志，记录推荐系统的运行记录"""
    id = models.BigAutoField(primary_key=True)
    user_id = models.IntegerField(null=True, blank=True, help_text='用户ID')
    box_id = models.IntegerField(null=True, blank=True, help_text='盲盒ID')
    recommendation_id = models.IntegerField(null=True, blank=True, help_text='推荐ID')
    recommendation_count = models.IntegerField(default=0, help_text='推荐数量')
    log_type = models.CharField(max_length=20, help_text='日志类型：generate, view, click')
    create_time = models.DateTimeField(default=timezone.now)
    
    class Meta:
        db_table = 'tb_recommendation_log'
        verbose_name = '推荐日志'
        verbose_name_plural = '推荐日志'
        managed = False
    
    def __str__(self):
        return f"推荐日志 {self.id} - {self.log_type}"
